<template>
  <div class="product-page">
    <el-form
      ref="formRef"
      :inline="true"
      :model="filterForm"
      class="search-form bg-bg_color w-full pl-4 pt-2 pb-2 overflow-auto"
    >
      <el-form-item label="关键词" prop="keyword">
        <el-input
          v-model.trim="filterForm.keyword"
          placeholder="标题 / 型号 / SKU"
          clearable
          class="w-[220px]!"
        />
      </el-form-item>

      <el-form-item label="分类" prop="category_id">
        <el-tree-select
          v-model="filterForm.category_id"
          :data="categoryOptions"
          :render-after-expand="false"
          check-strictly
          clearable
          filterable
          placeholder="全部分类"
          class="w-[220px]!"
        />
      </el-form-item>

      <el-form-item label="品牌" prop="brand_id">
        <el-select
          v-model="filterForm.brand_id"
          clearable
          filterable
          placeholder="全部品牌"
          class="w-[200px]!"
        >
          <el-option label="全部品牌" value="" />
          <el-option
            v-for="item in brandOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select
          v-model="filterForm.status"
          placeholder="全部状态"
          clearable
          class="w-[180px]!"
        >
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="审核状态" prop="audit_status">
        <el-select
          v-model="filterForm.audit_status"
          placeholder="全部审核状态"
          clearable
          class="w-[180px]!"
        >
          <el-option
            v-for="item in auditStatusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="卖家组织ID" prop="seller_org_id">
        <el-input
          v-model="filterForm.seller_org_id"
          placeholder="输入组织ID"
          clearable
          class="w-[160px]!"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon('ri:search-line')"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon('ep:refresh')" @click="resetForm">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <PureTableBar
      title="商品列表"
      :columns="columns"
      :tableRef="tableRef"
      @fullscreen="tableRef.value?.setAdaptive?.()"
      @refresh="onSearch"
    >
      <template #buttons>
        <el-button
          type="primary"
          :icon="useRenderIcon('ep:plus')"
          @click="openProductDialog()"
        >
          新增商品
        </el-button>
        <el-button
          type="success"
          :icon="useRenderIcon('ep:upload')"
          :disabled="multipleSelection.length === 0"
          @click="batchUpdateStatus(1)"
        >
          批量上架
        </el-button>
        <el-button
          type="warning"
          :icon="useRenderIcon('ep:download')"
          :disabled="multipleSelection.length === 0"
          @click="batchUpdateStatus(2)"
        >
          批量下架
        </el-button>
        <el-button
          type="danger"
          :icon="useRenderIcon('ep:minus')"
          :disabled="multipleSelection.length === 0"
          @click="batchUpdateStatus(3)"
        >
          批量禁用
        </el-button>
      </template>

      <template #default="{ size, dynamicColumns }">
        <PureTable
          ref="tableRef"
          v-loading="loading"
          row-key="id"
          :columns="dynamicColumns"
          :data="dataList"
          :size="size"
          :pagination="pagination"
          adaptive
          :adaptiveConfig="{ offsetBottom: 56 }"
          @selection-change="handleSelectionChange"
          @page-current-change="handlePageChange"
          @page-size-change="handleSizeChange"
        >
          <template #status="{ row }">
            <el-tag
              :type="statusTagType(row.status)"
              effect="plain"
              size="small"
            >
              {{ statusLabel(row.status) }}
            </el-tag>
          </template>

          <template #auditStatus="{ row }">
            <el-tag
              :type="auditStatusTagType(row.audit_status)"
              effect="plain"
              size="small"
            >
              {{ auditStatusLabel(row.audit_status) }}
            </el-tag>
          </template>

          <template #operation="{ row }">
            <el-button
              link
              type="primary"
              :icon="useRenderIcon('ep:edit')"
              @click="openProductDialog(row.id)"
            >
              编辑
            </el-button>
            <el-button
              link
              type="danger"
              :icon="useRenderIcon('ep:delete')"
              @click="deleteProduct(row.id)"
            >
              删除
            </el-button>
          </template>
        </PureTable>
      </template>
    </PureTableBar>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { useProductList } from "../hooks/useProductList";

defineOptions({ name: "ProductList" });

const {
  filterForm,
  loading,
  dataList,
  pagination,
  multipleSelection,
  columns,
  statusOptions,
  auditStatusOptions,
  brandOptions,
  categoryOptions,
  statusTagType,
  statusLabel,
  auditStatusTagType,
  auditStatusLabel,
  onSearch,
  resetForm,
  handlePageChange,
  handleSizeChange,
  handleSelectionChange,
  openProductDialog,
  deleteProduct,
  batchUpdateStatus
} = useProductList();

const formRef = ref();
const tableRef = ref();
</script>

<style scoped>
.product-page {
  padding: 12px;
}

.search-form :deep(.el-form-item) {
  margin-bottom: 12px;
}
</style>
